package com.ruoyi.web.controller.common;

import org.apache.poi.ss.usermodel.*;
import org.springframework.stereotype.Component;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;
@Component
public class ExcelLangReader {

    public static LinkedHashMap<String, String> dataMap = null;

    public static LinkedHashMap<String, String> getLangList() {

        if (null != dataMap) {
            return dataMap;
        }

        LinkedHashMap<String, String> dataMap = new LinkedHashMap<>();
        ExcelLangReader reader = new ExcelLangReader();
        try {
            //先写死完了在放配置文件
            dataMap = reader.readExcel("C:\\Users\\Administrator\\Desktop\\工作簿1.xlsx");
            System.out.println(dataMap);

        } catch (IOException e) {
            e.printStackTrace();
        }
        return dataMap;
    }

    public static LinkedHashMap<String, String> readExcel(String filePath) throws IOException {
        LinkedHashMap<String, String> dataMap = new LinkedHashMap<>();

        FileInputStream file = new FileInputStream(filePath);
        Workbook workbook = WorkbookFactory.create(file);
        Sheet sheet = workbook.getSheetAt(0);

        for (int i = 0; i < sheet.getLastRowNum() + 1; i++) {
            if (i == 0) {
                continue;
            }
            Cell keyCell = sheet.getRow(i).getCell(3);
            Cell valueCell = sheet.getRow(i).getCell(4);

            if (keyCell != null && valueCell != null) {
                String key = keyCell.getStringCellValue();
                String value = valueCell.getStringCellValue();
                dataMap.put(key, value);
            }
        }

        workbook.close();
        file.close();

        return dataMap;
    }


}
